/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.net.handler;

import game.GameInstance;
import java.nio.ByteBuffer;
import server.net.Client;
import server.net.ClientConnection;
import shared.net.protocol.PacketCreator;
import shared.util.Pair;
import game.Player;


/**
 *
 * @author nathanposlusny
 */
public class PlayerListRequestHandler implements ServerPacketHandler {
    @Override
    public int processPacket(Client client, ByteBuffer packet) {
        if (client == null || packet == null)
        {
            return -1;
        }
        ClientConnection connection = client.getConnection();
        if (!client.isLoggedIn()) {
            connection.queuePacketSend(PacketCreator.LOBBY
                    .getPlayerListFailureResponse("Error: You must be logged in to view players"));
            return -1;
        }
        
        Player [] players = client.getGameServer().getPlayerRegistry().getAllPlayers();
        String [] playerList = new String [players.length];
        for (int i=0; i< players.length; i++) {
            playerList[i] = players[i].getName();
        }
        
        connection.queuePacketSend(PacketCreator.LOBBY.getPlayerListResponse(playerList));    
        return 0;
        
    }
    
    
    
    
}
